package com.controls.ws;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;

import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.events.WebServiceCommunicatorListener;
import com.objects.Contact;
import com.objects.Group;
import com.objects.GroupMember;

public class GroupWSControl extends BaseWSControl {
	public GroupWSControl(Context cxt, WebServiceCommunicatorListener lsn) {
		super(cxt, lsn);
	}

	public boolean getGroups(String accessToken) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetGroups xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "</GetGroups>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.GET_GROUP, content);
		return true;
	}

	public boolean getGroupName(String accessToken, long groupID) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetGroupName xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GroupID>" + groupID
				+ "</GroupID>" + "</GetGroupName>" + "</soap:Body>"
				+ "</soap:Envelope>";

		fetch(WebServiceFlag.GET_GROUP_NAME, content);
		return true;
	}

	public boolean addGroup(String accessToken, String groupName) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<AddGroupName xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GroupName>"
				+ groupName + "</GroupName>" + "</AddGroupName>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.ADD_GROUP, content);
		return true;
	}

	public boolean deleteGroup(String accessToken, String groupIDs) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<DeleteGroupsFromList xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GroupIDs>"
				+ groupIDs + "</GroupIDs>" + "</DeleteGroupsFromList>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.DELETE_GROUP, content);
		return true;
	}

	public boolean editGroup(String accessToken, long groupID, String groupName) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<UpdateGroupName xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GroupID>" + groupID
				+ "</GroupID>" + "<GroupName>" + groupName + "</GroupName>"
				+ "</UpdateGroupName>" + "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.EDIT_GROUP_NAME, content);
		return true;
	}

	public boolean addGroupMember(String accessToken, long groupID,
			long contactID) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<AddGroupMember xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GroupID>" + groupID
				+ "</GroupID>" + "<ContactID>" + contactID + "</ContactID>"
				+ "</AddGroupMember>" + "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.ADD_GROUP_MEMBER, content);
		return true;
	}

	public boolean removeGroupMember(String accessToken, String memberIDs) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<DeleteGroupMembers xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GroupMemberIDs>"
				+ memberIDs + "</GroupMemberIDs>" + "</DeleteGroupMembers>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.DELETE_GROUP_MEMBER, content);
		return true;
	}

	public boolean getGroupMember(String accessToken, long groupID) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetGroupMembers xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GroupID>" + groupID
				+ "</GroupID>" + "</GetGroupMembers>" + "</soap:Body>"
				+ "</soap:Envelope>";

		fetch(WebServiceFlag.GET_GROUP_MEMBER, content);
		return true;
	}

	/**
	 * 
	 * @param xml
	 * @return
	 */

	public ArrayList<Group> parseGroups(String xml) {
		ArrayList<Group> items = new ArrayList<Group>();
		Document doc = getDomElement(xml);
		if (doc == null) {
			return items;
		}

		NodeList results = doc.getElementsByTagName("GetGroupsResult");
		JSONArray mArray = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mArray = new JSONArray(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mArray != null) {
			for (int i = 0; i < mArray.length(); i++) {
				try {
					JSONObject obj = mArray.getJSONObject(i);
					Group item = new Group();
					if (obj.has("GroupId")) {
						item.setID(obj.getLong("GroupId"));
					}
					if (obj.has("GroupName")) {
						item.setTitle(obj.getString("GroupName"));

					}
					items.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return items;
	}

	public ArrayList<GroupMember> parseGroupMembers(String xml) {
		ArrayList<GroupMember> items = new ArrayList<GroupMember>();
		Document doc = getDomElement(xml);
		if (doc == null) {
			return items;
		}

		NodeList results = doc.getElementsByTagName("GetGroupMembersResult");
		JSONArray mArray = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mArray = new JSONArray(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mArray != null) {
			for (int i = 0; i < mArray.length(); i++) {
				try {
					JSONObject obj = mArray.getJSONObject(i);
					GroupMember item = new GroupMember();
					if (obj.has("GroupMemberID")) {
						item.setID(obj.getLong("GroupMemberID"));
					}
					if (obj.has("GroupMemberFirstName")) {
						item.setFirstName(obj.getString("GroupMemberFirstName"));

					}
					if (obj.has("GroupMemberLastName")) {
						item.setLastName(obj.getString("GroupMemberLastName"));

					}
					if (obj.has("GroupMemberEmailAddress")) {
						item.setEmail(obj.getString("GroupMemberEmailAddress"));

					}
					items.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return items;
	}
	public ArrayList<Contact> parseGroupMemberContact(String xml) {
		ArrayList<Contact> items = new ArrayList<Contact>();
		Document doc = getDomElement(xml);
		if (doc == null) {
			return items;
		}

		NodeList results = doc.getElementsByTagName("GetGroupMembersResult");
		JSONArray mArray = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mArray = new JSONArray(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mArray != null) {
			for (int i = 0; i < mArray.length(); i++) {
				try {
					JSONObject obj = mArray.getJSONObject(i);
					Contact item = new Contact();
					if (obj.has("GroupMemberID")) {
						item.setID(obj.getLong("GroupMemberID"));
					}
					if (obj.has("GroupMemberFirstName")) {
						item.setFirstName(obj.getString("GroupMemberFirstName"));

					}
					if (obj.has("GroupMemberLastName")) {
						item.setLastName(obj.getString("GroupMemberLastName"));

					}
					if (obj.has("GroupMemberEmailAddress")) {
						item.setEmailAddress(obj.getString("GroupMemberEmailAddress"));

					}
					items.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return items;
	}


	public Group parseAddGroups(String xml) {
		Group item = new Group();
		item.setID(-1);
		Document doc = getDomElement(xml);
		if (doc == null) {
			return item;
		}

		NodeList results = doc.getElementsByTagName("AddGroupNameResult");
		JSONObject mObj = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mObj = new JSONObject(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mObj != null) {
			try {
				if (mObj.has("GroupID")) {
					item.setID(mObj.getLong("GroupID"));
				}
				if (mObj.has("Result")) {
					item.setTitle(mObj.getString("Result"));
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return item;
	}

	public String parseAction(String xml, String key) {
		String returnValue = "";
		Document doc = getDomElement(xml);
		if (doc == null) {
			return returnValue;
		}

		NodeList results = doc.getElementsByTagName(key);
		JSONObject mObj = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mObj = new JSONObject(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mObj != null) {
			try {

				if (mObj.has("ReturnValue")) {
					returnValue = mObj.getString("ReturnValue");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnValue;
	}

}
